package product.controller;


import com.feign.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import product.domain.Brand;
import product.domain.CategoryBrandRelation;
import product.service.CategoryBrandRelationService;
import org.springframework.web.bind.annotation.*;
import product.vo.BrandVo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 品牌分类关联(CategoryBrandRelation)表控制层
 *
 * @author panghu
 * @since 2023-03-04 21:09:17
 */
@RestController
@RequestMapping("product/categoryBrandRelation")
public class CategoryBrandRelationController {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @GetMapping("/catelog/list")
    public R getListByBrandId(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelation> categoryBrandRelationList = categoryBrandRelationService.getListByBrandId(brandId);
        return R.ok().put("data", categoryBrandRelationList);
    }

    @PostMapping("save")
    public R save(@RequestBody CategoryBrandRelation categoryBrandRelation) {
        categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable Long id) {
        boolean result = categoryBrandRelationService.removeById(id);
        return result ? R.ok("移除成功") : R.error("移除失败");
    }

    @GetMapping("/brands/list")
    public R relationBrandList(@RequestParam(value = "catId") Long catId) {
        // 此service的方法不直接返回vo，因为可能别人会用此方法
        List<Brand> brandList = categoryBrandRelationService.getBrandsByCatId(catId);
        List<BrandVo> brandVoList = brandList.stream().map(brand -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandName(brand.getName());
            brandVo.setBrandId(brand.getBrandId());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", brandVoList);
    }

}

